//
//  HMVisitorView.swift
//  WeiBo14
//
//  Created by HM on 16/3/3.
//  Copyright © 2016年 HM. All rights reserved.
//  用户的访客视图

import UIKit
import SnapKit

/// 协议
// 在Swift要使用weak修饰的代理,那么其遵守的协议需要继承于基协议 NSObjectProtocol
// 协议的可选方法的两个关键字: @objc & optional
@objc protocol HMVisitorViewDelegate: NSObjectProtocol{
    // 登录按钮点击
    optional func visitorView(visitorView: HMVisitorView, loginButtonDidCliked: UIButton);
}


class HMVisitorView: UIView {
    
    // 代理
    weak var delegate: HMVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 供外界设置当前访客视图内容的方法
    ///
    /// - parameter imageNamed: 当前大图标显示的图片
    /// - parameter message:    消息label显示的内容
    func setPageInfo(imageNamed: String?, message: String?){
        // 判断 imageName 是否为空,如果为nil,代表是首页
        if imageNamed == nil {
            // 代表是首页
            startAnimation()
        }else{
            // 不是首页,隐藏圆圈
            circleView.hidden = true
            // 设置图标内容
            iconView.image = UIImage(named: imageNamed!)
            messageLabel.text = message
        }
    }
    
    // 开启转圈动画
    private func startAnimation(){
        // 初始化动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 指动画的结束
        anim.toValue = 2 * M_PI
        // 重复次数
        anim.repeatCount = MAXFLOAT
        // 执行一次动画所需要的时长
        anim.duration = 20
        // 在切换页面或者应用退到后台之后,动画会停止
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    @objc private func loginButtonClick(button: UIButton) {
        delegate?.visitorView?(self, loginButtonDidCliked: button)
    }
    
    
    // MARK: - 懒加载控件
    
    // 大图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        // 行数
        label.numberOfLines = 0
        // 居中
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
    // 注册
    lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        // 添加点击事件
        button.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        return button;
    }()
    
    
    // 登录
    private lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        // 添加点击事件
        button.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        return button;
    }()
    
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}

// MARK: - 设置视图
extension HMVisitorView {
    /// 设置视图
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        // UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        // 1. 添加子控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 添加约束
        // 2.1
        iconView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self)
            make.center.equalTo(self)
        }
        // 2.2 圆圈
        circleView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(iconView.snp_centerX)
//            make.centerY.equalTo(iconView.snp_centerY)
            make.center.equalTo(iconView)
        }
        // 2.3 消息label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom)
            make.width.equalTo(224)
        }
        // 2.4 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        // 2.5 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(100)
        }
        // 2.6 遮罩
        maskIconView.snp_makeConstraints { (make) -> Void in
//            make.centerY.equalTo(iconView.snp_centerY)
//            make.centerX.equalTo(iconView.snp_centerX)
            make.center.equalTo(iconView)
        }
        
    }
    
    /// 设置视图
    private func setupUI2(){
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        // UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        // 1. 添加子控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        /**
        参数:
        - 1. 要约束的控件
        - 2. 约束的属性,比如中心,左边
        - 3. 相等,大于,小于 (关系)
        - 4. 相对谁来约束
        - 5. 约束的属性,比如中心,左边
        - 6. 乘以
        - 7. 偏移量
        */
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 2.2 圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        // 2.3 消息label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        // 2.4 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 2.5 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        
        // 2.6 遮罩
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
    }
}
